#!/usr/bin/python

import sys
import os
from utils import dict_read
from utils import dict_write
from utils import list_read
from utils import list_write
from utils import open_store
from utils import match_or
from utils import match_and

from httpmine import httpmine_callback_path_text
from httpmine import httpmine_config_filename
from httpmine import phrases_filename

textmine_config = 'textmine_config'
textmine_path_phrases = 'textmine.path.phrases'
textmine_path_phrases_sorted = 'textmine.path.phrases.sorted'
textmine_path_phrases_unique = 'textmine.path.phrases.unique'
textmine_path_phrases_counts = 'textmine.path.counts'
textmine_path_words = 'textmine.path.words'
textmine_path_words_sorted = 'textmine.path.words.sorted'
textmine_path_words_unique = 'textmine.path.words.unique'
textmine_path_words_counts = 'textmine.path.words'

textmine_config_filename = 'textmine_config'
phrases_sorted_filename = 'phrases_sorted.txt'
phrases_unique_filename = 'phrases_unique.txt'
phrases_counts_filename = 'phrases_counts.txt'
words_filename = 'words.txt'
words_sorted_filename = 'words_sorted.txt'
words_unique_filename = 'words_unique.txt'
words_counts_filename = 'words_counts.txt'

def configure():
    path = sys.argv[2] if len(sys.argv) > 2 else os.getcwd()
    httpmine_config = dict_read(os.path.join(path, httpmine_config_filename))
    path_text = httpmine_config[httpmine_callback_path_text]
    textmine_config = dict()
    path_phrases = os.path.join(path_text, phrases_filename)
    path_phrases_sorted = os.path.join(path_text, phrases_sorted_filename)
    path_phrases_unique = os.path.join(path_text, phrases_unique_filename)
    path_phrases_counts = os.path.join(path_text, phrases_counts_filename)
    path_words = os.path.join(path_text, words_filename)
    path_words_sorted = os.path.join(path_text, words_sorted_filename)
    path_words_unique = os.path.join(path_text, words_unique_filename)
    path_words_counts = os.path.join(path_text, words_counts_filename)

    textmine_config[textmine_path_phrases] = path_phrases
    textmine_config[textmine_path_phrases_sorted] = path_phrases_sorted
    textmine_config[textmine_path_phrases_unique] = path_phrases_unique
    textmine_config[textmine_path_phrases_counts] = path_phrases_counts
    textmine_config[textmine_path_words] = path_words
    textmine_config[textmine_path_words_sorted] = path_words_sorted
    textmine_config[textmine_path_words_unique] = path_words_unique
    textmine_config[textmine_path_words_counts] = path_words_counts
    dict_write(os.path.join(path, textmine_config_filename), textmine_config)
    print 'configure done!'     

def start():
    path = sys.argv[2] if len(sys.argv) > 2 else os.getcwd()
    textmine_config = dict_read(os.path.join(path, textmine_config_filename))
    path_phrases = textmine_config[textmine_path_phrases]
    path_phrases_sorted = textmine_config[textmine_path_phrases_sorted]
    path_phrases_unique = textmine_config[textmine_path_phrases_unique]
    path_phrases_counts = textmine_config[textmine_path_phrases_counts]
    path_words = textmine_config[textmine_path_words]
    path_words_sorted = textmine_config[textmine_path_words_sorted]
    path_words_unique = textmine_config[textmine_path_words_unique]
    path_words_counts = textmine_config[textmine_path_words_counts]

    command = "sort %s > %s" % (path_phrases, path_phrases_sorted)
    os.popen(command)

    f = open(path_phrases_sorted, 'r')
    u = open(path_phrases_unique, 'w')
    c = open(path_phrases_counts, 'w')
    w = open(path_words, 'w');
    curr = f.readline()
    nxt = f.readline()
    cnt = 1
    while nxt != '':
        # print "curr %s - nxt %s - cnt %d" % (curr, nxt, cnt)
        if curr == nxt:
            cnt = cnt + 1
        else:
            # print cnt
            u.write(curr)
            for s in curr.split():
                w.write(s + '\n')
            c.write(repr(cnt) + '\n')
            cnt = 1
        curr = nxt
        nxt = f.readline()
    f.close()
    u.close()
    c.close()
    w.close()

    command = "sort %s > %s" % (path_words, path_words_sorted)
    os.popen(command)

    f = open(path_words_sorted, 'r')
    u = open(path_words_unique, 'w')
    c = open(path_words_counts, 'w')
    curr = f.readline()
    nxt = f.readline()
    cnt = 1
    while nxt != '':
        # print "curr %s - nxt %s - cnt %d" % (curr, nxt, cnt)
        if curr == nxt:
            cnt = cnt + 1
        else:
            # print cnt
            u.write(curr)
            c.write(repr(cnt) + '\n')
            cnt = 1
        curr = nxt
        nxt = f.readline()
    f.close()
    u.close()
    c.close()

    print 'start done!'     


def update():
     print 'no update done!'     
